﻿using Dysoft.RabbitMQ.Helper;
using Dysoft.RabbitMQ.Model;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace Dysoft.RabbitMQ.Test.Client
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("【消费者】启动成功");

            var message = new MessageQueue();

            //订阅消息
            SubscribeRabbitMQMsg(message);

            //发布广播消息
            //PublishRabbitMQMsg(message);
        }

        /// <summary>
        /// 订阅消息
        /// </summary>
        static void SubscribeRabbitMQMsg(MessageQueue message)
        {
            #region MyRegion

            //订阅广播消息
            message.Subscribe("test.channel", (string data) => Console.WriteLine(data));
            message.Subscribe("test.dynamic.channel", (dynamic data) =>
            {
                Console.WriteLine($"接收成功：{data.message}");
            });

            message.Subscribe("Teacher.Insert", (MQMsgModel data) =>
            {
                Console.WriteLine($"【教师新增】接收成功：{data.ToJsonString()}");
            });
            message.Subscribe("Teacher.Update", (MQMsgModel data) =>
            {
                Console.WriteLine($"【教师修改】接收成功：{data.ToJsonString()}");
            });
            message.Subscribe("Teacher.Delete", (MQMsgModel data) =>
            {
                Console.WriteLine($"【教师删除】接收成功：{data.ToJsonString()}");
            });


            message.Subscribe("dysoft.queue.pwdChangeRemind", (dynamic data) =>
            {
                Console.WriteLine($"【密码修改消息】接收成功：{data.username}");
            });

            #endregion

            //订阅队列消息
            message.SubscribeQueue("dykj_mq_log", (MessageLogDTO data) =>
            {
                Console.WriteLine($"【消息日志】接收成功");

                //throw new DySoftException("数据保存处理异常！");
                Thread.Sleep(5000);

                Console.WriteLine($"【消息日志】操作成功：{data.ToJsonString()}");

                message.IsOperationOk = true;
            });

        }


        /// <summary>
        /// 发布消息
        /// </summary>
        static void PublishRabbitMQMsg(MessageQueue message)
        {
            #region RabbitMQ_Demo

            message.Publish("test.channel", "我发出了一条数据，时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

            Task.Delay(2000).ContinueWith((t) => message.Publish("test.channel", "延迟发送了一条数据，时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));

            Task.Delay(5000).ContinueWith((t) => message.Publish("test.dynamic.channel",
                new
                {
                    message = "延迟发送了一条匿名类型数据，时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
                }));

            #endregion

            #region 广播消息

            Task.Run(() =>
            {
                for (int i = 0; i < 10; i++)
                {
                    var teacherObj = new
                    {
                        msg = $"我发出了一条数据！——【{i}】",
                    };

                    var msgContent = new MQMsgModel()
                    {
                        MessageID = Guid.NewGuid().ToString("N"),
                        MessageTitle = "教师新增",
                        MessageContent = teacherObj,
                        MessageRouter = "Teacher.Insert",
                        MessageSource = "FounPlatform",
                        SendingTime = DateTime.Now      //消息队列时间戳
                    };

                    message.Publish("Teacher.Insert", msgContent);
                    Thread.Sleep(3000);
                }
            });

            #endregion

            #region 队列消息

            Task.Run(() =>
            {
                for (int i = 0; i < 10; i++)
                {
                    MessageLogDTO log = new MessageLogDTO();
                    log.LogID = Guid.NewGuid().ToString("N");
                    log.Sender = $"我发出了一条数据！——【{i}】";
                    log.SendTime = DateTime.Now;
                    log.MessageID = Guid.NewGuid().ToString("N");
                    log.SendContent = "消息同步日志记录";

                    Task.Delay(1000).ContinueWith((t) => message.PublishQueue("dykj_mq_log", log));
                }
            });
            #endregion
        }


    }
}
